package sample.Shapes;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import sample.Utils.IsInUtils;
import sample.Utils.RedrawUtils;
import sample.Utils.SplitShapArray;

import java.util.List;

public class Circle {

    public Canvas canvas;
    public GraphicsContext gc;

    public int draggingCircle = -1;
    public  double CIRCLE_SIZE = 100;

    public List<Object> totalList;
    List<ArrowLine> arrowLines;
    List<Circle> circles;
    List<Ellipse> ellipses;

    public double x;
    public double y;
    public double w;
    public double h;

    public Circle(GraphicsContext gc, Canvas canvas, double x, double y, double w, double h) {
        this.gc=gc;
        this.canvas=canvas;

        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;

        registerMouseEvents(canvas, gc);
        draw(gc,x,y,w,h);
    }

    public void setTotalList(List<Object> totalList){
        this.totalList=totalList;

        arrowLines= SplitShapArray.getArrowLines(totalList);
        circles= SplitShapArray.getCircles(totalList);
    }

    // 注册鼠标事件处理器
    private void registerMouseEvents(Canvas canvas, GraphicsContext gc) {
        canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, this::handleMousePressed); // 鼠标按下事件
        canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, e -> handleMouseDragged(e, gc)); // 鼠标拖动事件
        canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, e -> {
            draggingCircle = -1;
        });
    }

    // 处理鼠标按下事件
    private void handleMousePressed(MouseEvent event) {
        double mouseX = event.getX(); // 获取鼠标点击位置
        double mouseY = event.getY();

        if(IsInUtils.isPointInCircle(mouseX,mouseY,x,y,w,h)){
            System.out.println(111);
            draggingCircle=0;
        }
    }

    // 处理鼠标拖动事件
    private void handleMouseDragged(MouseEvent event, GraphicsContext gc) {
        double mouseX = event.getX(); // 获取鼠标点击位置
        double mouseY = event.getY();

        if (draggingCircle != -1) {
            x=mouseX-CIRCLE_SIZE/2;
            y=mouseY-CIRCLE_SIZE/2;

            for (int ii = 0; ii <arrowLines.size(); ii++) {
                if (IsInUtils.isPointInCircle(arrowLines.get(ii).lineEndX, arrowLines.get(ii).lineEndY, x, y, w,h)) {
                    arrowLines.get(ii).lineEndX = mouseX;
                    arrowLines.get(ii).lineEndY = mouseY;
                }

                if(IsInUtils.isPointInCircle(arrowLines.get(ii).lineStartX, arrowLines.get(ii).lineStartY, x, y, w,h)){
                    arrowLines.get(ii).lineStartX=mouseX;
                    arrowLines.get(ii).lineStartY=mouseY;
                }
            }
        }

        totalList=RedrawUtils.redraw(gc,totalList,arrowLines,circles,ellipses);
    }

    public void draw(GraphicsContext gc, double x, double y, double w, double h) {
        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;
        gc.setFill(Color.BLACK);
        gc.fillOval(x,y,w,h);
    }
}
